<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../static/Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../static/Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../static/Css/style.css" />
    <link rel="stylesheet" type="text/css" href="../static/layui/css/layui.css"/>
    <script type="text/javascript" src="../static/Js/jquery.js"></script>
    <script type="text/javascript" src="../static/Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../static/Js/bootstrap.js"></script>
    <script type="text/javascript" src="../static/Js/ckform.js"></script>
    <script type="text/javascript" src="../static/Js/common.js"></script>
    <!-- <script type="text/javascript" src="../static/layui/layui.js"></script> -->
    <script type="text/javascript" src="../static/layer/layer.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }
        th,td{
			text-align:center;
        }
        .raw{
        	margin: 0px 5px;
    		background-color: orangered;
    		border: none;
    		color: white;
        }
        .raw_div1{
            box-shadow: 0px 5px 5px rgba(0,0,0,0.1);
    		line-height: 50px;
    	}
    		
    	#rawSearchInput{
    	    line-height: 30px;
    		width: 255px;
    		margin: 0px 15px;
    		text-indent: 15px;
    	}


    </style>
</head>
<body>
	<div class="raw_div1">
	    <input  type="text" id="rawSearchInput" placeholder="输入物料名称进行查询" name="rolename" id="rolename"class="abc input-default" placeholder="" value="">&nbsp;&nbsp;  
    	<button type="button" id="rawSearchParams" class="btn btn-primary">查询</button>&nbsp;&nbsp; 
    	<button type="button" class="btn btn-success" id="addRawmaterial">新增物料</button>
	</div>   
	
	<table class="table table-bordered table-hover definewidth m10" >
		<colgroup>
	    	<col width="15%">
	    	<col width="8%">
	    	<col width="8%">
	    	<col width="8%">
	    	<col width="8%">
	    	<col width="8%">
	    	<col width="20%">
	    	<col width="15%">
	  	</colgroup>
    	<thead>
    		<tr>
		        <th>物料名称</th>
		        <th>责任人</th>
		        <th>责任人电话</th>
		        <th>物料总数</th>
		        <th>剩余数量</th>
		       	<th>出货数量</th>
		        <th>购货次数</th>
		        <th>出货次数</th>
		        <th>生成时间</th>
		        <th>操作</th>
	    	</tr>
	    </thead>
	    <tbody id="rawTbody"></tbody>
	</table>
<!-- <div class="inline pull-right page">
         </div> -->
</body>
</html>
<script type="text/javascript">
	
	$(document).ready(function(){
		var searchContent = $("#rawSearchInput").val();
		getRawmaterialList(searchContent);
	});
	
	function getRawmaterialList(searchContent){
		$.ajax({
			type:"post",
			dataType:"json",
			url:"/rawmaterial/getRawmaterialList",
			data:{"content":searchContent},
			success:function(result){
				if(result.ret == 1){
					var rl = result.list;
					$("#rawTbody").empty();
					for(var i = 0 ;i < rl.length; i++){
	                    var raw = rl[i];
	                    var str = "<tr class='raw_"+raw.id+"'>";
	                    str += "<td>"+raw.name+"</td>";
	                    str += "<td>"+raw.person+"</td>";
	                    str += "<td>"+raw.phone+"</td>";
	                    str += "<td>"+raw.num+"</td>";
	                    str += "<td>"+raw.surplus+"</td>";
	                    str += "<td>"+raw.outnum+"</td>";
	                    str += "<td>"+raw.purchaseNum+"</td>";
	                    str += "<td>"+raw.shipmentNum+"</td>";
	                    var time = new Date(parseInt(raw.createDate)).toLocaleString().replace(/:\d{1,2}$/,' ');
	                    str += "<td class='time'>"+time+"</td>";
	                    str += "<td>";
	                    str += "<button value='"+raw.id+"' class='btn btn-primary raw rawDel'>删除</button>";
	                    str += "<button value='"+raw.id+"' class='btn btn-primary ord rawEdit' style='background:#2196f3'>修改</button>";
	                    //str += "<button value='"+order.id+"' class='btn btn-primary ord downOrder' style='background:#3f51b5'>下单</button>";
	                    //<button type="button" id="cusSearchParams" class="btn btn-primary">查询</button>
	                    str += "</td>";
	                    str += "</tr>";
	                    $("#rawTbody").append(str);
		    		}
				}else{
					ErrTips(result.msg);
				}
			},
			error:function(result){
				ErrTips("服务器错误");
			}
		})
	};
	
	var editIndex;
	var currentId = "";
	$(document).on('click','.rawEdit',function () {
		var rawId = $(this).attr('value');
		currentId = rawId;
		$.ajax({
	      	type:"post",
	      	dataType:"json",
	      	url:"/rawmaterial/getRawmaterialById",
	      	data:{"id":currentId},	
	      	success:function(result){
	            if(result.ret == 1){
	            	var cus = result.cus;
	                editIndex = layer.open({
	                    type: 1,
	                    title: '修改客户信息',
	                    content: "<div style='line-height: 20px;margin: 10px 20px;width: 300px;font-size: 13px'>" +
	                    "<span style='width: 80px'>物料名称：</span><input value='"+cus.name+"' placeholder='物料名称' maxlength='10' id='cusConName' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' type='text'/></br> " +
	                    "<span style='width: 80px'>物料总数：</span><input value='"+cus.num+"' placeholder='物料总数'  id='cusnum' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' type='text'/></br> " +
	                    "<span style='width: 80px'>剩余数量：</span><input value='"+cus.surplus+"' placeholder='剩余数量' maxlength='25' id='cussurplus' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' type='text'/></br> " +
	                    "<span style='width: 80px'>出货数量：</span><input value='"+cus.outnum+"' placeholder='出货数量' maxlength='50' id='cusoutnum' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' type='text'/></br> " +
	                    "<span style='width: 80px'>购货次数：</span><input value='"+cus.purchaseNum+"' placeholder='购货次数' maxlength='50' id='cuspurchaseNum' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' type='text'/></br> " +
	                    "<span style='width: 80px'>出货次数：</span><input value='"+cus.shipmentNum+"' placeholder='出货次数' maxlength='50' id='cusshipmentNum' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' type='text'/></br> " +
	                    "<span id='cus_editSub' style='cursor: pointer;display: block;width: 80px;height: 30px;text-align: center;margin: 20px auto;background: cornflowerblue;line-height: 30px;color: white;border-radius: 5px'>确定</span>"+
	                    "</div>",
	                    anim: '1',
	                    style: 'position:fixed;left:0; padding:10px 0; border:none;top: 50px;margin: 20px;right: 0px;border-radius: 10px;'
	                });
	            }else{
	            	ErrTips(result.msg);
	            }
	          },
	          error:function(result){
	        	  ErrTips("服务器错误");
	          }
	      })
	});
	
	
	$(document).on('click','#cus_editSub',function () {
		
        var name = $("#cusConName").val();
        var num = $("#cusnum").val();
        var surplus = $("#cussurplus").val();
        var outnum = $("#cusoutnum").val();
        var purchaseNum = $("#cuspurchaseNum").val();
        var shipmentNum = $("#cusshipmentNum").val();
        
        if(currentId == ""){
        	ErrTips("id不能为空");
            return;
        }
        console.log($.trim(name) == "");
        console.log( $.trim(num) == "");
        console.log($.trim(surplus) == "");
        console.log( $.trim(outnum) == "");
        console.log($.trim(purchaseNum) == "");
        console.log($.trim(shipmentNum) == "");
        if($.trim(name) == "" || $.trim(num) == "" || $.trim(surplus) == "" || $.trim(outnum) == ""||$.trim(purchaseNum) == ""||$.trim(shipmentNum) == ""){
        	ErrTips("有未填写的信息");
            return;
        }
        
        $.ajax({
	      	type:"post",
	      	dataType:"json",
	      	url:"/rawmaterial/updateRawmaterial",
	      	data:{"name":name,"num":num,"surplus":surplus,"outnum":outnum,"id":currentId,"purchaseNum":purchaseNum,"shipmentNum":shipmentNum},	
	      	success:function(result){
	            if(result.ret == 1){
	            	TureTips(result.msg);
	            	layer.close(editIndex);
	            	$("#cusTbody").empty();
	            	getCustomerList();
	            	//setTimeout("location.reload()",1500);
	            }else{
	            	ErrTips(result.msg);
	            }
	          },
	          error:function(result){
	        	  ErrTips("服务器错误");
	          }
	      })
    });
	
	$("#rawSearchParams").on('click',function(){
		var searchContent = $("#rawSearchInput").val();
		if($.trim(searchContent) == ""){
	   		ErrTips("搜索内容不能为空");
	        return;
	    }
		getRawmaterialList(searchContent);
		
	})
	
	var addIndex;
	$(document).on('click','#addRawmaterial',function () {
		//弹出层
        addIndex = layer.open({
            type: 1,
            title: '添加物料',
            content: "<div style='line-height: 20px;margin: 10px 20px;width: 300px;font-size: 13px'>" +
            "<span style='width: 65px;display:inline-block;'>名称：</span><input placeholder='物料名称' maxlength='25' id='rawName' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' type='text'/></br> " +
            "<span style='width: 65px;display:inline-block;'>责任人：</span><input placeholder='责任人' maxlength='9' id='dutyer' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' /></br> " +
            "<span style='width: 65px;display:inline-block;'>物料数量：</span><input placeholder='物料数量' maxlength='9' id='rawNum' style='padding: 5px 10px;outline: none;border: none; border-bottom: 1px solid #ccc;width: 200px' type='text'/></br> " +
            "<span id='raw_submit' style='cursor: pointer;display: block;width: 80px;height: 30px;text-align: center;margin: 20px auto;background: cornflowerblue;line-height: 30px;color: white;border-radius: 5px'>确定</span>"+
            "</div>",
            anim: '1',
            style: 'position:fixed;left:0; padding:10px 0; border:none;top: 50px;margin: 20px;right: 0px;border-radius: 10px;'
        });
	});
	
	$(document).on('click','#raw_submit',function () {
		var name = $("#rawName").val();
		var dutyer = $("#dutyer").val();
		var num = $("#rawNum").val();
		
		if($.trim(name) == "" || $.trim(dutyer) == "" || $.trim(num) == ""){
			ErrTips("有未填写的信息");
			return;
		}
		
		$.ajax({
	      	type:"post",
	      	dataType:"json",
	      	url:"/rawmaterial/insertRawmaterial",
	      	data:{"name":name,"dutyer":dutyer,"num":num},	
	      	success:function(result){
	            if(result.ret == 1){
	        		layer.close(addIndex);
	            	TureTips(result.msg);
	            	getRawmaterialList("");
	            }else{
	            	ErrTips(result.msg);
	            }
	          },
	          error:function(result){
	        	  ErrTips("服务器错误");
	          }
	      })
	})
	
	$(document).on('click','.rawDel',function () {
		var rawId = $(this).attr('value');
		layer.confirm('确认删除吗？', {
			icon:3,
			btn: ['确定','取消'] //按钮
			}, function(){
				deleteRawmaterial(rawId);
			});
	});
	
	function deleteRawmaterial(rawId){
		$.ajax({
	      	type:"post",
	      	dataType:"json",
	      	url:"/rawmaterial/deleteRawmaterial",
	      	data:{"id":rawId},	
	      	success:function(result){
	            if(result.ret == 1){
	            	TureTips(result.msg);
	            	$(".raw_"+rawId).remove();
	            }else{
	            	ErrTips(result.msg);
	            }
	          },
	          error:function(result){
	        	  ErrTips("服务器错误");
	          }
	      })
	};


	function ErrTips(msg) {
		layer.msg(msg,{icon:7,time:1000});
    }
	
	function TureTips(msg) {
		layer.msg(msg,{icon:1,time:1500});
	}

</script>